/*-
 * Copyright (c) 2009-2010 Shang-Rong Cai. All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 *
 * THIS SOFTWARE IS PROVIDED BY THE AUTHORS AND CONTRIBUTORS ``AS IS'' AND
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 * ARE DISCLAIMED.  IN NO EVENT SHALL AUTHOR OR CONTRIBUTORS BE LIABLE
 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
 * SUCH DAMAGE.
 *
 * $ Bitmap: bitmap.h    Rev. 1.0.2    2010-10-20T11:11+08:00 $
 */

#ifndef _BITMAP
#define _BITMAP

#include <stdlib.h>
#include <stdint.h>

#define BITMAP_BLOCK_SIZE (size_t) 8


#define BITMAP_BIT_NONE 0
#define BITMAP_BIT_MARK 1


#define BITMAP_MARK_0 (uint8_t) 0x80
#define BITMAP_MARK_1 (uint8_t) 0x40
#define BITMAP_MARK_2 (uint8_t) 0x20
#define BITMAP_MARK_3 (uint8_t) 0x10
#define BITMAP_MARK_4 (uint8_t) 0x08
#define BITMAP_MARK_5 (uint8_t) 0x04
#define BITMAP_MARK_6 (uint8_t) 0x02
#define BITMAP_MARK_7 (uint8_t) 0x01


#define BITMAP_NONE_0 (uint8_t) 0x7f
#define BITMAP_NONE_1 (uint8_t) 0xbf
#define BITMAP_NONE_2 (uint8_t) 0xdf
#define BITMAP_NONE_3 (uint8_t) 0xef
#define BITMAP_NONE_4 (uint8_t) 0xf7
#define BITMAP_NONE_5 (uint8_t) 0xfb
#define BITMAP_NONE_6 (uint8_t) 0xfd
#define BITMAP_NONE_7 (uint8_t) 0xfe

#define BITMAP_ALL_NONE (uint8_t) 0x00
#define BITMAP_ALL_MARK (uint8_t) 0xff


typedef struct
{
    size_t bksz;  /* Block size */
    size_t size;  /* Array size */
    uint8_t *list;
} bitmap_t;


extern int bitmap_init(bitmap_t *bm, size_t sz);

extern int bitmap_dest(bitmap_t *bm);

extern int bitmap_set_mark(bitmap_t *bm, size_t ps);

extern int bitmap_set_none(bitmap_t *bm, size_t ps);

extern int bitmap_check(bitmap_t *bm, size_t ps, int *bl);

extern int bitmap_get_marks(bitmap_t *bm, size_t *mkz);

extern int bitmap_get_nones(bitmap_t *bm, size_t *nnz);

extern int bitmap_sync(bitmap_t *bma, bitmap_t *bmb);


extern int bitmap_all_mark(bitmap_t *bm);

extern int bitmap_all_none(bitmap_t *bm);

extern int bitmap_count_marks(bitmap_t *bm, size_t *mkz);

extern int bitmap_count_nones(bitmap_t *bm, size_t *nnz);

#endif
